package main;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;

import world.Camera;
import world.Player;
import world.World;

/**
 * Main class to control the program.
 * 
 * @author mstichel
 * 
 */
public class Main {

	public static final int WIDTH = 800, HEIGHT = 600;

	World w;
	Camera c;
	Player p;

	public enum Team {
		BLUE, RED
	}

	public Main() {
		try {
			Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
			Display.create();
		} catch (LWJGLException e) {
			e.printStackTrace();
		}

		w = new World();
		p = new Player(140f, 140f, Team.RED);
		c = new Camera(p);

		w.spawnPlayer(p);

		long last = System.nanoTime();

		while (!Display.isCloseRequested()) {
			long current = System.nanoTime();
			float passed = (current - last) / 1000000000f;
			last = current;
			render();
			tick(passed);
		}
		Display.destroy();
	}

	public void render() {
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();
		c.useView();
		glPushMatrix();
		{
			w.draw();
		}
		glPopMatrix();
		Display.update();
	}

	public void tick(float t) {
		float speed = 200f;
		boolean up = false, right = false, down = false, left = false;
		if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
			up = true;
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
			right = true;
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
			down = true;
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
			left = true;
		}
		
		int dw = -Mouse.getDWheel();
		
		c.move(0, 0, dw);

		if (up && !down) {
			p.move(0, -speed * t);
		} else if (down && !up) {
			p.move(0, speed * t);
		}
		if (left && !right) {
			p.move(-speed * t, 0);
		} else if(right && !left) {
			p.move(speed * t, 0);
		}
	}

	public static void main(String[] args) {
		new Main();
	}

}
